
/*** START CODE ***/


// Preamble
// ----------------------------
capture log close
clear all
*version 14.2
set more off
set matsize 800


/*Pairwise regressions*/

use "${Data_Clean}/spatial_RD_regdata_placebo.dta", clear

local delta_demog "delta_population delta_pop_density delta_income"
local delta_dist "cons i.groupid#c.IOU_dist i.groupid#c.muni_dist"
local other_cov "delta_fuel_economy_mean delta_hybrid_share delta_luxury_share delta_mud_hh_share"
local iou_fe "PGE_dum SCE_dum SDGE_dum"
local dist_km = 5
local dist_m = `dist_km'*1000
local dist_condition "distance_btw_cbg0 < `dist_m'"
local price_var "delta_p_e_active delta_P_gas"

local icempg = 30
local BEVmpkwh = 4

gen delta_p_e_active_permile = delta_p_e_active / `BEVmpkwh'
gen delta_P_gas_permile = delta_P_gas / `icempg'

label var delta_p_e_active_permile "$\Delta$ Marg. Price (cents/mile)"
label var delta_P_gas_permile "$\Delta$ Gas Price (cents/mile)"

local price_var_permile "delta_p_e_active_permile delta_P_gas_permile"


* Merge in the muni-level data
merge m:1 cbg0 cbg1 using "${Boundaries}/cbg_muni.dta"

eststo clear
gen right_cbg = .
gen delta_ind = .
gen delta_inc = .
gen delta_pop = .
gen delta_pop_den = .
* Differencing for income
replace right_cbg = .
replace delta_ind = .
replace right_cbg = (income1 > income0)
replace delta_ind = .
replace delta_ind = income1 - income0 if right_cbg == 1
replace delta_ind = income0 - income1 if right_cbg == 0
replace delta_pop = population1 - population0 if right_cbg == 1
replace delta_pop = population0 - population1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_cbg == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_cbg == 0

eststo model_inc_cbg: reghdfe delta_share_BEV delta_ind delta_pop delta_pop_den delta_P_gas right_cbg `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)

	
* Differencing for population
replace right_cbg = .
replace delta_ind = .
replace right_cbg = (population1 > population0)
replace delta_ind = .
replace delta_ind = population1 - population0 if right_cbg == 1
replace delta_ind = population0 - population1 if right_cbg == 0
replace delta_inc = income1 - income0 if right_cbg == 1
replace delta_inc = income0 - income1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_cbg == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_cbg == 0

eststo model_pop_cbg: reghdfe delta_share_BEV delta_ind delta_inc delta_pop_den delta_P_gas right_cbg `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)
	
* Differencing for population density
replace right_cbg = .
replace delta_ind = .
replace right_cbg = (pop_density1 > pop_density0)
replace delta_ind = .
replace delta_ind = pop_density1 - pop_density0 if right_cbg == 1
replace delta_ind = pop_density0 - pop_density1 if right_cbg == 0
replace delta_inc = income1 - income0 if right_cbg == 1
replace delta_inc = income0 - income1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_cbg == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_cbg == 0

eststo model_pop_den_cbg: reghdfe delta_share_BEV delta_ind delta_inc delta_pop delta_P_gas right_cbg `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)
	
	
gen right_muni = .
* Differencing for income
replace right_muni = .
replace delta_ind = .
replace right_muni = (muni_income1 > muni_income0)
replace delta_ind = .
replace delta_ind = income1 - income0 if right_muni == 1
replace delta_ind = income0 - income1 if right_muni == 0
replace delta_pop = population1 - population0 if right_cbg == 1
replace delta_pop = population0 - population1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_muni == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_muni == 0

eststo model_inc_muni: reghdfe delta_share_BEV delta_ind delta_pop delta_pop_den delta_P_gas right_muni `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)
	
* Differencing for population
replace right_muni = .
replace delta_ind = .
replace right_muni = (muni_population1 > muni_population0)
replace delta_ind = .
replace delta_ind = population1 - population0 if right_muni == 1
replace delta_ind = population0 - population1 if right_muni == 0
replace delta_inc = income1 - income0 if right_cbg == 1
replace delta_inc = income0 - income1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_muni == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_muni == 0

eststo model_pop_muni: reghdfe delta_share_BEV delta_ind delta_inc delta_pop_den delta_P_gas right_muni `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)
	
* Differencing for population density
replace right_muni = .
replace delta_ind = .
replace right_muni = (muni_pop_den1 > muni_pop_den0)
replace delta_ind = .
replace delta_ind = pop_density1 - pop_density0 if right_muni == 1
replace delta_ind = pop_density0 - pop_density1 if right_muni == 0
replace delta_inc = income1 - income0 if right_cbg == 1
replace delta_inc = income0 - income1 if right_cbg == 0
replace delta_pop_den = pop_density1 - pop_density0 if right_cbg == 1
replace delta_pop_den = pop_density0 - pop_density1 if right_cbg == 0
replace delta_share_BEV = .
replace delta_share_BEV = share_BEV1 - share_BEV0 if right_muni == 1
replace delta_share_BEV = share_BEV0 - share_BEV1 if right_muni == 0

eststo model_pop_den_muni: reghdfe delta_share_BEV delta_ind delta_inc delta_pop delta_P_gas right_muni `other_cov', ///
	absorb(cons i.groupid#c.distance_util0 i.groupid#c.distance_util1) vce(cluster cbg0 cbg1)

* Making the table
esttab using "${ResultsOut}/Tables/main_six.tex", replace ///
	booktabs se label keep(right_*) ///
	mtitle("Income" "Population" "Population Density" "Income" "Population" "Population Density") ///
	nonotes star(* 0.10 ** 0.05 *** 0.01)	
